import { ListService, PagedAndSortedResultRequestDto } from '@abp/ng.core';
import { Component, OnInit } from '@angular/core';
import { EntityListComponent } from '../../core';
import { ListBottonsConfigService, ThemeService } from '../../services';
import { TenantDto, TenantService } from '../proxy';
import { RefFeatureService } from '../ref-feature/ref-feature.service';
import { RefTenantDetailService } from '../ref-tenant-detail/ref-tenant-detail.service';
import { TenantListService } from './tenant.service';

@Component({
  selector: 'app-tenant',
  templateUrl: './tenant.component.html',
  providers: [ListService, ListBottonsConfigService],
})
export class TenantComponent extends EntityListComponent<TenantDto> {
  featureButton = {
    name: 'Host功能管理',
    fn: () => {
      this.refFeatureService.show({ providerName: 'T' }).subscribe();
    },
    visable: true,
    enabled: true,
  };

  constructor(
    public readonly list: ListService<PagedAndSortedResultRequestDto>,
    public buttonConfigService: ListBottonsConfigService,
    public tenantListService: TenantListService,
    public theme: ThemeService,
    public tenantDetailService: RefTenantDetailService,
    public tenantService: TenantService,
    public refFeatureService: RefFeatureService
  ) {
    super(list, buttonConfigService, tenantListService);

    this.buttonConfigService.buttonsConfig.push({
      buttonGroup: [this.featureButton],
      visable: true,
    });
  }

  public new() {
    this.tenantDetailService.show().subscribe(model => {
      if (!!model) {
        this.tenantService
          .create({
            ...model,
            adminEmailAddress: `${model.name}@qq.com`,
            adminPassword: '1q2w3E*',
          })
          .subscribe(() => this.list.get());
      }
    });
  }

  public view(model: TenantDto, event?: Event) {
    event?.stopPropagation();
    this.tenantDetailService.show(model).subscribe(model => {
      if (!!model) {
        this.tenantService.update(model['id'], model).subscribe(() => this.list.get());
      }
    });
  }

  public editFeatures(tenant?: TenantDto, event?: Event) {
    event?.stopPropagation();
    this.refFeatureService.show({ providerName: 'T', keyModel: tenant }).subscribe();
  }
}
